package com.lcm.weam.controller.algo;

import com.lcm.weam.entity.algo.SurfaceWaterEva;
import com.lcm.weam.entity.resp.Result;
import com.lcm.weam.entity.resp.ResultCode;
import com.lcm.weam.service.algo.EvaluateService;
import com.lcm.weam.util.AliOSSUtil;
import com.lcm.weam.util.ExcelHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 算法接口
 * 1、批量评价地表水质量
 * 2、批量评价地下水饮用水质量
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/algo")
public class EvaluateController {

    @Autowired
    private EvaluateService evaluateService;

    //根据输入文件评价指标
    @RequestMapping(value = "/SurfaceWaterEva/file", method = RequestMethod.POST)
    public Result SurfaceWaterEva(MultipartFile file, int baseLevel) throws IOException {
        if (file.isEmpty()){
            return new Result(ResultCode.FAIL);
        }
        List<SurfaceWaterEva> evaList = evaluateService.SurfaceWaterEva(file, baseLevel);
        return new Result(ResultCode.SUCCESS, evaList);
    }

    @RequestMapping(value = "/SurfaceWaterEva/download", method = RequestMethod.POST)
    public void SurfaceWaterEvaDownload(@RequestBody List<SurfaceWaterEva> evalist, HttpServletResponse response) throws IOException {
        ByteArrayOutputStream outputStream = ExcelHandler.writeSurfaceWaterEvaResult(evalist);
        AliOSSUtil.download(outputStream, response, UUID.randomUUID().toString() + ".xlsx");
    }

}
